﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Windows.Forms;

public partial class Management_ChargeNurse_Yeucau : System.Web.UI.Page
{
    public static string[] status = {
                                        "Đợi duyệt",
                                        "Chọn người nhận",                                        
                                        "Không duyệt",
                                        "Đang nhận thuốc",
                                        "Xác nhận thuốc",
                                        "Xong",
                                   };

    public static DataTable tblThuoc;
    public static int thuocID;
    static int tabID=0;
    static int IDStaff;
    static int currentRequisitionID;
    static int currentWard;

    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            tblThuoc = new DataTable();
            thuocID = 0;
            tblThuoc.Columns.Add("ID",typeof(int));
            tblThuoc.Columns.Add("Tenthuoc", typeof(string));
            tblThuoc.Columns.Add("Soluong", typeof(int));
            tblThuoc.Columns.Add("Donvitinh", typeof(string));
            tblThuoc.Columns.Add("IDDrug", typeof(int));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IDStaff = ((Authentication)Session[GlobalConstant.LOGIN_STATE]).userID;
            danhsachyeucau.SelectParameters["IDStaff"].DefaultValue = IDStaff.ToString();
            GridView1.DataBind();
            currentWard = Management_Danhsachnhanvien.getIDWardFromIDStaff(IDStaff);
            vattuDS.SelectParameters["IDWard"].DefaultValue=currentWard.ToString();
        }
        TabContainer1.ActiveTabIndex = tabID;
    }

    protected void XemchitietYeucau(object sender, EventArgs args)
    {
        string ID = (sender as LinkButton).CommandArgument;
        chitietyeucau.SelectParameters["IDRequisition"].DefaultValue = ID;
        currentRequisitionID = Int32.Parse(ID);
        GridView2.DataBind();

        Danhsachnhanvien1.loadStaffOf(currentWard);

        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT IDStaffReceive, DateReceive, Accepted, Reason FROM tbRequisition WHERE ID="+ID;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        int IDStaffReceive = reader.GetInt32(0);
        DateTime DateReceive = reader.GetDateTime(1);
        int Accepted = reader.GetInt32(2);
        string reason = reader.GetString(3);
        reader.Close();
        con.Close();

        switch (Accepted)
        {
            case 0:
                pnNguoinhan.Visible = false;
                pnNgaynhan.Visible = false;
                pnNhanthatbai.Visible = false;
                break;
            case 1:
                pnNguoinhan.Visible = true;                
                pnNgaynhan.Visible = false;
                pnNhanthatbai.Visible = false;
                break;
            case 2:
                pnNguoinhan.Visible = false;                
                pnNgaynhan.Visible = false;
                pnNhanthatbai.Visible = true;
                tbLydo.Text = reason;
                break;
            case 3:
                pnNguoinhan.Visible = true;
                Danhsachnhanvien1.setSelectedStaffID(IDStaffReceive);
                Danhsachnhanvien1.setEnabled(false);
                btNguoinhan.Visible = false;             
                pnNgaynhan.Visible = false;
                pnNhanthatbai.Visible = false;
                break;
            case 4:
                pnNguoinhan.Visible = true;
                Danhsachnhanvien1.setEnabled(false);
                btNguoinhan.Visible = false;
                pnNgaynhan.Visible = true;
                lbNgaynhan.Text = DateReceive.ToString("dd-MM-yyyy");
                btNgaynhan.Visible = true;
                pnNhanthatbai.Visible = false;
                break;
            case 5:
                pnNguoinhan.Visible = true;
                Danhsachnhanvien1.setEnabled(false);
                btNguoinhan.Visible = false;
                pnNgaynhan.Visible = true;
                lbNgaynhan.Text = DateReceive.ToString("dd-MM-yyyy");
                btNgaynhan.Visible = false;
                pnNhanthatbai.Visible = false;
                break;
        }
        pnChitiet.Visible = true;
        lbMaYeucau.Text = ID;

    }
    protected void closePanel(object sender, EventArgs e)
    {
        pnChitiet.Visible = false;
    }

    protected void XoaThuoc(object sender, EventArgs e)
    {
        int ID = Int32.Parse((sender as LinkButton).CommandArgument);
        int i = -1;
        foreach (DataRow dr in tblThuoc.Rows)
        {
            i++;
            if ((int)dr[0]==ID)
            {
                tblThuoc.Rows.RemoveAt(i);
                break;
            }
        }
        GridView3.DataSource = tblThuoc;
        GridView3.DataBind();
    }

    protected void btThem_Click(object sender, EventArgs e)
    {   
        //check if this thuoc is in datatable
        bool isExist=false;
        int id=-1;
        string thuoc = liThuoc.CurrentName;
        int soluong = Int32.Parse(tbSoluong.Text);

        foreach (DataRow dr in tblThuoc.Rows)
        {
            id++;
            if (dr[1].Equals(thuoc))
            {
                isExist=true;
                break;
            }
        }
        if (isExist)
        {
            soluong += (int)(tblThuoc.Rows[id][2]);
            tblThuoc.Rows[id][2] = soluong;
        }
        else
        {
            tblThuoc.Rows.Add(thuocID,thuoc,soluong,liThuoc.CurrentUnitType,liThuoc.SelectedID);
            thuocID++;        
        }
        tbSoluong.Text = "";
        GridView3.DataSource = tblThuoc;
        GridView3.DataBind();        
    }
    protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
    {
        tabID = TabContainer1.ActiveTabIndex;
    }
    protected void btReset_Click(object sender, EventArgs e)
    {
        tblThuoc.Rows.Clear();
        GridView3.DataBind();
        tbSoluong.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (tblThuoc.Rows.Count > 0)
        {
            //create querry
            string sql;
            SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
            conn.Open();            
            sql = "INSERT INTO tbRequisition(RequisitionDate, IDStaff, IDStaffReceive, DateReceive, Accepted) VALUES(@p1, @p2, 0, @p3, 0)";
            SqlCommand com = new SqlCommand(sql, conn);
            com.Parameters.AddWithValue("@p1", DateTime.Today);
            com.Parameters.AddWithValue("@p2", IDStaff);
            com.Parameters.AddWithValue("@p3", DateTime.ParseExact("01-01-1900","dd-MM-yyyy", CultureInfo.InvariantCulture));
            com.ExecuteNonQuery();
      
            sql = "SELECT ID FROM tbRequisition WHERE ID = IDENT_CURRENT('tbRequisition')";
            com = new SqlCommand(sql,conn);
            SqlDataReader reader = com.ExecuteReader();
            reader.Read();
            int lastID = reader.GetInt32(0);
            reader.Close();

            sql="";
            foreach (DataRow dr in tblThuoc.Rows)
            {
                sql+="INSERT INTO tbRequisitionDetail VALUES("+lastID+","+dr["IDDrug"]+","+dr["Soluong"]+"); ";
            }
            com = new SqlCommand(sql,conn);
            com.ExecuteNonQuery();
            conn.Close();

            btReset_Click(null, null);
            GridView1.DataBind();
        }
    }

    protected void btNguoinhan_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        conn.Open();
        string sql = "UPDATE tbRequisition SET IDStaffReceive=" + Danhsachnhanvien1.getCurrentStaffID()+ ", Accepted=3 WHERE ID="+currentRequisitionID;
        SqlCommand com = new SqlCommand(sql, conn);
        com.ExecuteNonQuery();
        conn.Close();
        GridView1.DataBind();
    }

    protected void btNgaynhan_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        conn.Open();
        string sql = "UPDATE tbRequisition SET Accepted=5 WHERE ID="+currentRequisitionID;
        SqlCommand com = new SqlCommand(sql, conn);
        com.ExecuteNonQuery();                

        //cap nhat kho thuoc cua khoa
        sql = "";
        foreach (GridViewRow dr in GridView2.Rows)
        {
            //kiem tra thuoc co chua?
            int IDthuoc = liThuoc.getIDDrugByName(dr.Cells[1].Text);
            int soluongthuoc =Int32.Parse(dr.Cells[2].Text);
            string sql2 = "SELECT * FROM tbWardDrug WHERE IDDrug="+IDthuoc;
            com = new SqlCommand(sql2, conn);
            SqlDataReader reader = com.ExecuteReader();
            if (reader.Read())
            {
                sql += "UPDATE tbWardDrug SET Quantity=Quantity+" + soluongthuoc + " WHERE IDWard=" + currentWard + " AND IDDrug=" + IDthuoc + "; ";
            }
            else
            {
                sql += "INSERT INTO tbWardDrug VALUES("+currentWard+","+IDthuoc+","+soluongthuoc+"); ";
            }
            reader.Close();            
        }
        com = new SqlCommand(sql, conn);
        com.ExecuteNonQuery();
        conn.Close();

        closePanel(null, null);
        GridView1.DataBind();
    }

    public void Xemvattu(object sender, EventArgs e)
    {
        int ID = Int32.Parse((sender as LinkButton).CommandArgument);
        //select
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT * FROM tbDrug WHERE ID=" + ID;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            lbName.Text = reader.GetString(2);
            lbUnitType.Text = reader.GetString(4);
            lbUseMethod.Text = reader.GetString(5);
            lbCost.Text = reader.GetDouble(6).ToString();
            tbDesc.Text = reader.GetString(3);
        }
        reader.Close();
        con.Close();

        pnChitietvattu.Visible = true;
    }

    protected void radio1_CheckedChanged(object sender, EventArgs e)
    {
        vattuDS.SelectParameters["Type"].DefaultValue = "1";
        gvDanhsachvattu.DataBind();
    }
    protected void radio2_CheckedChanged(object sender, EventArgs e)
    {
        vattuDS.SelectParameters["Type"].DefaultValue = "0";
        gvDanhsachvattu.DataBind();
    }
    protected void btCloseVt_Click(object sender, EventArgs e)
    {
        pnChitietvattu.Visible = false;
    }
}